package com.maxd.controller;

import com.maxd.adapter.IgnoreSecurity;
import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.bean.activity.ActivityJoinData;
import com.maxd.bean.activity.JoinActivityReq;
import com.maxd.bean.member.JoinMemberReq;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.ActivityJoinRecord;
import com.maxd.model.MemberInfo;
import com.maxd.model.RwActivity;
import com.maxd.model.UserMemberRecord;
import com.maxd.service.activity.IRwActivityService;
import com.maxd.service.member.IMemberService;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(value="戎威少年会员管理", tags = {"戎威少年会员管理"})
@RequestMapping(value = "/member")
public class MemberController {

    private static final Logger log = LoggerFactory.getLogger(MemberController.class);
    @Autowired
    private IMemberService memberService;

    @PostMapping("/save-member")
    @ApiOperation("新增/修改会员信息")
    public Result<Void> save(@RequestBody @Validated MemberInfo memberInfo) {
        AdminUtils.checkLogin();
        memberService.save(memberInfo);
        return ResultUtil.success();
    }

    @IgnoreSecurity
    @GetMapping("/member-list")
    @ApiOperation("查询会员信息列表，按创建时间倒序排序")
    public Result<List<MemberInfo>> list() {
        List<MemberInfo> memberInfos = memberService.list();
        return ResultUtil.success(memberInfos);
    }

    @PostMapping("/join-member")
    @ApiOperation("购买会员")
    public Result<UserMemberRecord> join(@RequestBody @Validated JoinMemberReq req) {
        UserMemberRecord record = memberService.joinMember(req);
        return ResultUtil.success(record);
    }

    @GetMapping("/my-member-info")
    @ApiOperation("商城查询本人会员信息")
    public Result<List<UserMemberRecord>> myMemberInfo() {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        PageRes<UserMemberRecord> pageRes = memberService.userMemberInfo(0, 100, userId, null, null);
        return ResultUtil.success(pageRes.getRows());
    }

    @GetMapping("/activity-join-list")
    @ApiOperation("内管查询会员购买记录")
    public Result<PageRes<UserMemberRecord>> joinList(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                  @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                  @ApiParam("活动ID") @RequestParam(value = "activityId", required = false) Long memberId,
                                                  @ApiParam("用户ID") @RequestParam(value = "userId", required = false) Long userId,
                                                  @ApiParam("状态 0-初始化 1-付款中 2-完成 3-取消") @RequestParam(value = "status", required = false) String status) {
        AdminUtils.checkLogin();
        PageRes<UserMemberRecord> pageRes = memberService.userMemberInfo(pageNo, pageSize, userId, memberId, status);
        return ResultUtil.success(pageRes);
    }

}